package com.farewell.hotel.controller;

import com.farewell.hotel.mapper.OneMenuMapper;
import com.farewell.hotel.pojo.OneMenu;
import com.farewell.hotel.pojo.User;
import com.farewell.hotel.service.OneMenuService;
import com.farewell.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Farewell
 * @date 2021/4/21
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @Autowired
    private OneMenuService oneMenuService;

    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    public String login(String username, String pwd, HttpSession httpSession) {
        User user = userService.findByPwd(username, pwd);
        if (null != user) {
            List<OneMenu> oneMenuList = oneMenuService.findOneMenuByUserId(user.getId());
            httpSession.setAttribute("oneMenuList", oneMenuList);
            httpSession.setAttribute("username", username);
            return "index.jsp";
        }else {
            // 错误, 返回登陆界面并通知
            httpSession.setAttribute("errorMessage", "请输入账户名以及密码");
            return "login.jsp";
        }
    }
}
